package com.ParkingManager.app.web;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ParkingManager.app.service.StatisticsService;

@RestController
public class StatisticsController {
	@Autowired
	private StatisticsService statisticsService;

	// 统计停车 状况
	@RequestMapping("/statistics/getCarCount")
	public List<Object> getCarCount(String year, String month, String day, Integer parkingId) {
	
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parkingId", parkingId);
		List<Object> list = new ArrayList<Object>();
		if (!(day == "null" || day.isEmpty())) {
			String timeStr = year + "-" + month + "-" + day + " ";
			for (int i = 0; i < 24; i++) {
				String timeStr1 = "";
				String timeStr2 = "";
				if (i < 10) {
					timeStr1 = timeStr + "0" + Integer.toString(i) + ":00:00.0";
					if(i==9)
					{
						timeStr2 = timeStr + Integer.toString(i + 1) + ":00:00.0";
					}else{
					timeStr2 = timeStr + "0" + Integer.toString(i + 1) + ":00:00.0";
					}
				} else if (i == 23) {
					timeStr1 = timeStr + Integer.toString(i) + ":00:00.0";
					timeStr2 = timeStr + Integer.toString(i) + ":59:59.0";
				} else {
					timeStr1 = timeStr + Integer.toString(i) + ":00:00.0";
					timeStr2 = timeStr + Integer.toString(i + 1) + ":00:00.0";
				}
				Timestamp time0 = Timestamp.valueOf(timeStr1);
				Timestamp time1 = Timestamp.valueOf(timeStr2);
				map.put("beginTime", time0);
				map.put("endTime", time1);
				list.add(statisticsService.getCarCount(map));
			}

		} else if (!(month == "null" || month.isEmpty())) {
			String timeStr = year + "-" + month + "-";
			int y = Integer.parseInt(year);
			int m = Integer.parseInt(month);
			if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) { // 平闰年判断算法
				if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
					for (int i = 1; i < 32; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
						} else if (i == 31) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getCarCount(map));
					}
				} else if (m == 4 || m == 6 || m == 9 || m == 11) {
					for (int i = 1; i < 31; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
						} else if (i == 30) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getCarCount(map));
					}
				} else if (m == 2) {
					for (int i = 1; i < 30; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
						} else if (i == 29) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getCarCount(map));
					}

				}
			} else {
				if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
					for (int i = 1; i < 32; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
						} else if (i == 31) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getCarCount(map));
					}
				} else if (m == 4 || m == 6 || m == 9 || m == 11) {
					for (int i = 1; i < 31; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
						} else if (i == 30) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getCarCount(map));
					}
				} else if (m == 2) {
					for (int i = 1; i < 29; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
						} else if (i == 28) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getCarCount(map));
					}

				}
			}

		} else if (!(year == "null"||year.isEmpty())) {
			String timeStr = year + "-" ;
			for (int i = 1; i < 13; i++) {
				String timeStr1 = "";
				String timeStr2 = "";
				if (i < 10) {
					timeStr1 = timeStr + "0" + Integer.toString(i) + "-01 00:00:00.0";
					if(i==9)
					{
						timeStr2 = timeStr + Integer.toString(i + 1) + "-01 00:00:00.0";
					}else{
						timeStr2 = timeStr + "0" + Integer.toString(i + 1) + "-01 00:00:00.0";
					}						
				} else if (i == 12) {
					timeStr1 = timeStr + Integer.toString(i) + "-01 00:00:00.0";
					timeStr2 = timeStr + Integer.toString(i) + "-31 23:59:59.0";
				} else {
					timeStr1 = timeStr + Integer.toString(i) + "-01 00:00:00.0";
					timeStr2 = timeStr + Integer.toString(i + 1) + "-01 00:00:00.0";
				}
				Timestamp time0 = Timestamp.valueOf(timeStr1);
				Timestamp time1 = Timestamp.valueOf(timeStr2);
				map.put("beginTime", time0);
				map.put("endTime", time1);
				list.add(statisticsService.getCarCount(map));

			}
		}
		return list;
	}

	// 统计收入
	@RequestMapping("/statistics/getIncome")
	public List<Object> getIncome(String year,String month,String day,Integer parkingId) {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("parkingId", parkingId);
		List<Object> list = new ArrayList<Object>();
		if (!day.isEmpty()) {
			String timeStr = year + "-" + month + "-" + day + " ";
			for (int i = 0; i < 24; i++) {
				String timeStr1 = "";
				String timeStr2 = "";
				if (i < 10) {
					timeStr1 = timeStr + "0" + Integer.toString(i) + ":00:00.0";
					if(i==9)
					{
						timeStr2 = timeStr + Integer.toString(i + 1) + ":00:00.0";
					}else{
						timeStr2 = timeStr + "0" + Integer.toString(i + 1) + ":00:00.0";
					}	
					
				} else if (i == 23) {
					timeStr1 = timeStr + Integer.toString(i) + ":00:00.0";
					timeStr2 = timeStr + Integer.toString(i) + ":59:59.0";
				} else {
					timeStr1 = timeStr + Integer.toString(i) + ":00:00.0";
					timeStr2 = timeStr + Integer.toString(i + 1) + ":00:00.0";
				}
				Timestamp time0 = Timestamp.valueOf(timeStr1);
				Timestamp time1 = Timestamp.valueOf(timeStr2);
				map.put("beginTime", time0);
				map.put("endTime", time1);
				list.add(statisticsService.getIncome(map));
			}

		} else if (!month.isEmpty()) {
			String timeStr = year + "-" + month + "-";
			int y = Integer.parseInt(year);
			int m = Integer.parseInt(month);
			if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) { // 平闰年判断算法
				if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
					for (int i = 1; i < 32; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr  + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
							
						} else if (i == 31) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getIncome(map));
					}
				} else if (m == 4 || m == 6 || m == 9 || m == 11) {
					for (int i = 1; i < 31; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr  + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
						} else if (i == 30) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getIncome(map));
					}
				} else if (m == 2) {
					for (int i = 1; i < 30; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr  + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
						} else if (i == 29) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getIncome(map));
					}

				}
			} else {
				if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
					for (int i = 1; i < 32; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr  + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
						} else if (i == 31) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getIncome(map));
					}
				} else if (m == 4 || m == 6 || m == 9 || m == 11) {
					for (int i = 1; i < 31; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr  + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
						} else if (i == 30) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getIncome(map));
					}
				} else if (m == 2) {
					for (int i = 1; i < 29; i++) {
						String timeStr1 = "";
						String timeStr2 = "";
						if (i < 10) {
							timeStr1 = timeStr + "0" + Integer.toString(i) + " 00:00:00.0";
							if(i==9)
							{
								timeStr2 = timeStr  + Integer.toString(i + 1) + " 00:00:00.0";
							}else{
								timeStr2 = timeStr + "0" + Integer.toString(i + 1) + " 00:00:00.0";
							}	
						} else if (i == 28) {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i) + " 23:59:59.0";
						} else {
							timeStr1 = timeStr + Integer.toString(i) + " 00:00:00.0";
							timeStr2 = timeStr + Integer.toString(i + 1) + " 00:00:00.0";
						}
						Timestamp time0 = Timestamp.valueOf(timeStr1);
						Timestamp time1 = Timestamp.valueOf(timeStr2);
						map.put("beginTime", time0);
						map.put("endTime", time1);
						list.add(statisticsService.getIncome(map));
					}

				}
			}

		} else if (!year.isEmpty()) {
			String timeStr = year + "-" ;
			for (int i = 1; i < 13; i++) {
				String timeStr1 = "";
				String timeStr2 = "";
				if (i < 10) {
					timeStr1 = timeStr + "0" + Integer.toString(i) + "-01 00:00:00.0";
					if(i==9)
					{
						timeStr2 = timeStr  + Integer.toString(i + 1) + "-01 00:00:00.0";
					}else{
						timeStr2 = timeStr + "0" + Integer.toString(i + 1) + "-01 00:00:00.0";
					}	
					
				} else if (i == 12) {
					timeStr1 = timeStr + Integer.toString(i) + "-01 00:00:00.0";
					timeStr2 = timeStr + Integer.toString(i) + "-31 23:59:59.0";
				} else {
					timeStr1 = timeStr + Integer.toString(i) + "-01 00:00:00.0";
					timeStr2 = timeStr + Integer.toString(i + 1) + "-01 00:00:00.0";
				}
				Timestamp time0 = Timestamp.valueOf(timeStr1);
				Timestamp time1 = Timestamp.valueOf(timeStr2);
				map.put("beginTime", time0);
				map.put("endTime", time1);
				list.add(statisticsService.getIncome(map));

			}
		}
		return list;
	}

}
